/**
 * 公用方法mixin
 */
const {toast, dateFormat, setStorageSync, getStorageSync} = require("../../utils/utils");
const {store} = require("../../store/index");

module.exports = {
  data() {
    return {
      isWeChat: process.env.UNI_PLATFORM === 'wechat' || process.env.UNI_PLATFORM === 'mp-weixin',
      windowWidth: uni.getSystemInfoSync().windowWidth,
      windowHeight: uni.getSystemInfoSync().windowHeight
    }
  },
  onLoad() {
    // 更新顶部导航栏信息
    this.updateCustomBarInfo()
  },
  filters: {
    formatDate: function (value) {
      return dateFormat(new Date(value),"YYYY-MM-DD HH:mm:ss");
    },
    formatDateTime: function (value) {
      return dateFormat(new Date(value));
    },
    handlePic: function (value) {
	  if(!value){return ''}
      if (value.startsWith('http://') || value.startsWith('https://')) {
        return value;
      }
      return 'http://shortplay-static.5884.cn'+value;
    }
  },
  methods: {
    async reloadUserInfo () {
      let res = await this.$api.getMyUserInfo();
      let userinfo = res.userinfo
      return userinfo
    },
    copyText(txt) {
      console.log("复制" + txt)
      uni.setClipboardData({
        data: txt || "",
        success() {
          toast("复制成功")
        }
      })
    },
    getTotalBalance() { //金豆总计：余额+赠送
      let userInfo = this.vuex_user;
      let balance = userInfo.balance | 0;
      let giftBalance = this.getGiftBalance();
      return balance + giftBalance;
    },
    getGiftBalance() { // 赠送金额
      let balanceTotal=0;
      //有赠送金豆
      let now = Date.now()/1000;
      if (this.vuex_user.balance_gift>0 && (this.vuex_user.balance_gift_time==0 || now<this.vuex_user.balance_gift_time)) {
        balanceTotal+=this.vuex_user.balance_gift
      }
      return balanceTotal;
    },
    checkVip() {
      const currentTimestamp = Math.floor(new Date().getTime()/1000);
      const isVip = this.vuex_user.plus_type;
      const vipExpireTime = this.vuex_user.plus_expire_time; // 假设这是一个已知的 VIP 过期时间戳
      return isVip === 1 && vipExpireTime > currentTimestamp;
    },
    //调用客服
    openCustomerServiceChat() {
      let appInfo = this.getAppInfo()
      wx.openCustomerServiceChat({
        extInfo: {
          url: appInfo.workwxurl,
        },
        corpId: appInfo.workwxappid,
        success(res) {
        }
      })
    },
    // 跳转
    tn(e) {
      console.log(e)
      if (e.includes('kefu')) {
        this.openCustomerServiceChat()
        return;
      }
      if (e.includes('/pages/index/index') ||
          e.includes('/pages/tabbar/watching/watching') ||
          e.includes('/pages/tabbar/my/my') ||
          e.includes('/pages/tabbar/tj/tj')
      ) {
        uni.switchTab({
          url: e
        })
        return
      }
      if (e == "/pages/index/index?path=record") {
        uni.reLaunch({
          url: e
        })
        return;
      }
      uni.navigateTo({
        url: e,
      });
    },
    //外部跳转
    jump(url) {
      // #ifdef APP-PLUS
      console.log("app-plus jump url:" + url)
      plus.runtime.openURL(url);
      // #endif
      // #ifdef H5
      console.log("h5 jump url:" + url)
      window.location.href = url;
      // #endif
    },
    async getVideoDetail(tvId) {
      let params =  {
        tv_id:tvId
      }
      return await this.$api.getTvInfo(params)
    },
    async toVideoDetail(item) {
      let jumpSeries = item.series;
      this.$t.vuex('currentTv',item)
      let url ="/pages_sub/player/player?tvId="+item.tv_id
      if (jumpSeries) {
        url = '/pages_sub/player/player?tvId=' + item.tv_id + "&episode=" + jumpSeries;
      }
      uni.navigateTo({
        url: url
      })
    },
    // 点击左上角返回按钮时触发事件
    goBack() {
      // 通过判断当前页面的页面栈信息，是否有上一页进行返回，如果没有则跳转到首页
      const pages = getCurrentPages()
      if (pages && pages.length > 0) {
        const firstPage = pages[0]
        if (pages.length == 1 && (!firstPage.route || firstPage.route != 'pages/index/index')) {
          uni.reLaunch({
            url: '/pages/index/index'
          })
        } else {
          uni.navigateBack({
            delta: 1
          })
        }
      } else {
        uni.reLaunch({
          url: '/pages/index/index'
        })
      }
    },
    // 更新顶部导航栏信息
    async updateCustomBarInfo() {
      if (!this.$t) {
        return
      }
      // 获取vuex中的自定义顶栏的高度
      let customBarHeight = this.vuex_custom_bar_height
      let statusBarHeight = this.vuex_status_bar_height
      // 如果获取失败则重新获取
      if (!customBarHeight) {
        try {
          const navBarInfo = await this.$t.updateCustomBar()
          customBarHeight = navBarInfo.customBarHeight
          statusBarHeight = navBarInfo.statusBarHeight
        } catch (e) {
          setTimeout(() => {
            this.updateCustomBarInfo()
          }, 10)
          return
        }
      }
      // 更新vuex中的导航栏信息
      // this.$t.vuex('vuex_status_bar_height', statusBarHeight)
      // this.$t.vuex('vuex_custom_bar_height', customBarHeight)
    },
  }
}
